$(document).ready(function(){
	searchKeyword();
});

function searchKeyword(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/goods-keyword/get-keyword',
            type:'post',
            data:function(d){
                d.goods_keyword_name = $('#goods_keyword_name').val();
            },
            error:getError
        },
        aoColumns: [
        	{ "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "name", "bSortable": false },
            { "data": "sort", "bSortable": true },
            { "data": "use_flg", "bSortable": false },
//            { "data": "is_publish", "bSortable": false }
        ],
        columnDefs: [
        	{
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="changeKeyword('+ full.id +',\''+full.name+'\',\''+full.sort+'\',\''+full.use_flg+'\')">修改</a>';
                }
            },
            {
                "targets": [4],
                "data": "use_flg",
                "render": function(data, type, full) {
                    return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input style="width:20px;height:20px;" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check" type="checkbox"></span>';
                }
            },
//            {
//                "targets": [5],
//                "data": "use_flg",
//                "render": function(data, type, full) {
//                    if(data == 1){
//                    	return '<span class="text-success">已同步</span>';
//                    }else{
//                    	return '<a href="javascript:" class="'+(data == 1 ? 'text-danger' : '')+'" onclick="changeKeywordStatus('+ full.id +',1,2)">同步</a>';
//                    }
//            	}
//            }
                
        ],
        order: [[ 3, "asc"]]
    });
}

function table_list_reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}

function changeKeyword(id,name,sort,use_flg){
	$('#keyword-modal').modal('show');
	$('#keyword_id').val(id);
	$('#name').val(name);
	$('#sort').val(sort == 'null' ? '' : sort);
	if(!use_flg){
		$('#use_flg').val(0);
	}
}

function changeKeywordStatus(id,status,type){
	if(type == 1){
		title = '确定禁用此关键字吗？禁用后关键字将取消同步';
		if(status == 1){
			title = '确定启用此关键字吗？';
		}
	}else{
		title = '确定同步此关键字吗？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/goods-keyword/change-keyword-status',{id:id,status:status,type:type},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(status == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

$('#table_list').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeKeywordStatus(id,use_flg,1);
});

$('#reset_button').on('click',function(){
	$('#goods_keyword_name').val('');
	table_list_reload();
});

$('.ajax_post_tag').on('click',function(){
	var form = $('#keyword_form');
    var name = $('#name').val();
    if(name == ''){
        $.jBox.error('请输入关键字', '错误');
        return false;
    }
    var sort = $('#sort').val();
    if(sort == ''){
        $.jBox.error('请输入关键字排序编号', '错误');
        return false;
    }
    $.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#keyword-modal').modal('hide');
                window.setTimeout(function () { table_list_reload(); }, 1000);
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
	}
});